package com.jboost.modules.app.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jboost.common.api.vo.Result;
import com.jboost.modules.app.dto.BomItem;
import com.jboost.modules.app.dto.huixin.HxProductDetailDto;
import com.jboost.modules.app.util.StrategyFactory;
import com.jboost.modules.system.entity.FhdBom;
import com.jboost.modules.system.entity.FhdProductDz;
import com.jboost.modules.system.entity.HxMj;
import com.jboost.modules.system.entity.HxProduct;
import com.jboost.modules.system.mapper.FhdBomMapper;
import com.jboost.modules.system.mapper.FhdProductMapper;
import com.jboost.modules.system.mapper.HxMjMapper;
import com.jboost.modules.system.mapper.HxProductMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "客户管理")
@RestController
@RequestMapping("/hx")
@Slf4j
@AllArgsConstructor
public class HxController {


    HxProductMapper fhdProductMapper;
    HxMjMapper hxMjMapper;
    @PostMapping(value = "/orderInfo")
    @ApiOperation(value = "订单包装清单", notes = "订单包装清单")
    public void orderInfo(@RequestBody JSONObject jsonObject) throws Exception {

        StrategyFactory.invokeStrategy(jsonObject,"HxOrderQuery");
    }

    @GetMapping(value = "/mj" )
    @ApiOperation(value = "模具", notes = "模具")
    public Result<List<Map>> mj(@RequestParam(name = "hxxh_list",required = false,defaultValue = "") String hxxh_list,
                                @RequestParam(name = "mzmin_list",required = false,defaultValue = "") String mzmin_list,
                                @RequestParam(name = "mzmax_list",required = false,defaultValue = "") String mzmax_list) throws Exception {

        List<Map> maps = Lists.newArrayList();

        String[] hxxh = hxxh_list.split(",");

        String[] mzmin = mzmin_list.split(",");
        String[] mzmax = mzmax_list.split(",");


        for (int i =0;i<hxxh.length;i++) {
            Map<String,Object> map = new HashMap();
            map.put("hxxh",hxxh[i]);
            map.put("mzmin",mzmin[i]);
            map.put("mzmax",mzmax[i]);
          maps.add(buildData(map));
        }

        return Result.OK(maps);
    }

    private Map buildData(Map<String, Object> map) {

        List<HxMj> hxMjs1 = hxMjMapper.selectList(Wrappers.lambdaQuery(HxMj.class)
                .eq(HxMj::getMjmc, map.get("hxxh")).le(HxMj::getKmmz, map.get("mzmax"))
                .ge(HxMj::getKmmz, map.get("mzmin")).eq(HxMj::getMjzt, "正常"));
//        Map<Date, List<HxMj>> collect = hxMjs.stream().collect(Collectors.groupingBy(HxMj::getCreateTime));
//        Optional<Date> sorted = collect.keySet().stream().sorted(new Comparator<Date>() {
//            @Override
//            public int compare(Date o1, Date o2) {
//                return o2.compareTo(o1);
//            }
//
//        }).findFirst();
        if (CollectionUtil.isNotEmpty(hxMjs1)){
            map.put("sl",hxMjs1.stream().map(HxMj::getSycn).filter(Objects::nonNull).count());
            map.put("zl",hxMjs1.stream().map(HxMj::getSycn).filter(Objects::nonNull).mapToDouble(e->Double.parseDouble(e)).sum());
        }else {
            map.put("sl",0);
            map.put("zl",0);
        }

        return map;

    }

}
